package com.znsd.utils;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 
 * @ClassName: UploadimgServlet
 * @Description: TODO(验证码)
 * @author Liuyl
 * @date 2019年8月21日
 *
 */
@RequestMapping("/code")
@Controller
public class CodeControllor extends HttpServlet {
	private static final long serialVersionUID = 1L;
	// 生成图片的宽度
	private static int width = 75;
	// 生成图片的高度
	private static int height = 25;
	// 随机对象
	Random random = new Random();
	// 生成的数据字典
	String[] codes = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i",
			"j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };

	// 随机颜色
	private Color getRandColor(int fc, int bc) {
		if (fc > 255)
			fc = 255;
		if (bc > 255)
			bc = 255;
		int r = fc + random.nextInt(bc - fc);
		int g = fc + random.nextInt(bc - fc);
		int b = fc + random.nextInt(bc - fc);
		return new Color(r, g, b);
	}

	@RequestMapping("/picture")
	public void getCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 设置编码防止页面出现中文乱码
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		// 设置response的输出格式
		response.setContentType("image/jpeg");
		// 设置不被浏览器缓存
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		// 创建一个图片
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		// 创建一个画布对象
		Graphics g = image.getGraphics();
		// 绘制矩形,指定宽度,高度
		g.fillRect(0, 0, width, height);
		// 绘制背景颜色
		g.setColor(getRandColor(200, 255));
		// 绘制边框
		g.setColor(getRandColor(160, 200));
		g.drawRect(0, 0, width - 1, height - 1);
		// 随即产生200条干扰线
		g.setColor(getRandColor(120, 160));
		for (int i = 0; i < 200; i++) {
			// x的起点
			int x = random.nextInt(width);
			// y的起点
			int y = random.nextInt(height);
			// x的终点
			int xl = random.nextInt(12);
			// y的终点
			int yl = random.nextInt(12);
			g.drawLine(x, y, x + xl, y + yl);
		}
		// 设置字体
		g.setFont(new Font("宋体", Font.BOLD, 25));
		// 随机生存的验证码(4位数)
		String sRand = "";
		for (int i = 0; i < 4; i++) {
			String rand = codes[random.nextInt(codes.length)];
			sRand += rand;
			g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
			// 调用函数出来的颜色相同，可能是因为种子太接近，所以只能直接生成
			g.drawString(rand, 13 * i + 14, 20);
		}
		// 将图片保存到输出流
		ImageIO.write(image, "jpeg", response.getOutputStream());
		HttpSession session = request.getSession();
		session.setAttribute("scode", sRand);
		System.out.println("----------" + sRand);
		g.dispose();
	}
}
